layui.use(['dtree'], function(){
  var dtree = layui.dtree;
  var table = layui.table;
  var form = layui.form;
  var checkRoleId;
  var lastMenuIdsArr;
  
  
  //初始化状态下拉框
  $("#roleStatusSelect").initSelectDict();
  
  initTable({
	 elem: '#role-t'
	,toolbar: '#role-t-toolbar'
    ,url: ctx+'/sys/role/getPage'
    ,cols: [[
       {field:'roleId', title: 'ID', hide: true}
      ,{field:'roleName', minWidth:150, title: '角色名', sort: true}
      ,{field:'roleCode', minWidth:150, title: '角色编码', sort: true}
      ,{field:'remark', width:200, title: '备注',fixed: 'right'}
      ,{field:'roleSort', width:100, title: '等级排序', sort: true}
      ,{field:'roleStatus', width:80, title: '状态'
    	  ,templet: function(d){
    		  return transDict("roleStatus",d.roleStatus);
    	   }
      }
      ,{
			title: '操作',
			align: 'center',
			minWidth: 180,
			toolbar: '#role-t-tool',
			fixed: 'right'
		}
    ]]
  });
  
  //条件搜索
  $("#roleSearch").click(function(){
	    var searchData = $("#roleSearchForm").serializeObject();
		layui.table.reload('role-t', {
			where : searchData,
			page : {
				curr : 1
			}
		});
  })
  
   table.on('row(role-t)', function(obj){
	checkRoleId = obj.data.roleId;
	obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
    //选中radio样式
    //obj.tr.find('i[class="layui-anim layui-icon"]').trigger("click");
	 
	//点击加载菜单树
	  var menuTree = dtree.render({
		  elem: "#menuTree",
		  url: ctx+"/sys/role/getRoleMenu?id="+checkRoleId,
		  method: "get",
		  dataStyle: "layuiStyle",  
		  dataFormat: "list",  
		  response:{message:"msg",statusCode:200}, 
		  menubar: true, 
		  checkbar: true, 
		  checkbarType: "all" 
		  ,initLevel: 999
		  ,done: function(data, obj, first){
		    if(first) {
		      $("#menuSearch").unbind("click");
		      $("#menuSearch").click(function(){
		        var value = $("#menuSearchName").val();
		        if(value){
		          var flag = menuTree.searchNode(value); 
		          if (!flag) {layer.msg("该名称节点不存在！", {icon:5});}
		        } else {
		        	menuTree.menubarMethod().refreshTree(); 
		        }
		      });
		      //获取初始选中值
		      var lastMenuData = dtree.getCheckbarNodesParam("menuTree");
		  	  lastMenuIdsArr = getSplitArr(lastMenuData,'nodeId');
		    }
		 }
	  });
	  
	//绑定节点点击事件
	  dtree.on("node(menuTree)", function(obj){
	    if(!obj.param.leaf){
	      var $div = obj.dom;
	      menuTree.clickSpread($div);  
	    } else {
	      layer.msg("叶子节点就不展开了");
	    }
	  });
	  
	  $("#menuSearch").click(function(){
		  var input = $("#menuSearchName").val();
		  menuTree.fuzzySearch(input);
	  });
   
   }); 
  
  
  //头工具栏事件
  table.on('toolbar(role-t)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id);
    switch(obj.event){
      case 'addRole':
		var url = ctx+"/sys/role/addView";
		$.get(url, {}, function(res){
			if(!isJSON(res)){
				var index = layer.open({
					type: 1,
					title: '添加',
					skin: 'layui-layer-rim', 
					area: ['30%', '55%'], 
					offset: ['5%', '20%'],
			        content: res,
					btn: [],
					end: function(){
						tableReload();
					}
			  		,maxmin: true
			  });
			  layer.full(index);
			}
		  });
      break;
    }
    })
  
  //监听工具条
  table.on('tool(role-t)', function(obj){
    var data = obj.data;
    var id = data.roleId;
    if(obj.event === 'del'){
    	layer.confirm('确定删除吗？', function(index){
    		ajaxCommit({
    			 url : ctx+"/sys/role/del?id="+id
    			,successfunc: function(){ tableReload() }
    		})
        });
    } else if(obj.event === 'edit'){
		var url = ctx+"/sys/role/editView?id="+id;
		$.get(url, {}, function(res){
			if(!isJSON(res)){
			  var index = layer.open({
					type: 1,
					title: '编辑',
					skin: 'layui-layer-rim', 
					area: ['30%', '55%'],
					offset: ['5%', '20%'],
			        content: res,
					btn: [],
					end: function(){
						tableReload();
					}
			  		,maxmin: true
			  });
			  layer.full(index);
			}
		  });
    }
  });
  
  $("#saveRoleMenu").click(function(){
	  if(null==checkRoleId || '' == checkRoleId){
		  layer.alert("请选择一条记录");
		  return false;
	  }
	  var data = dtree.getCheckbarNodesParam("menuTree");
	  var idsArr = getSplitArr(data,'nodeId');
	  var params = {
		  "roleId":checkRoleId
		 ,"menuIds":idsArr
		 ,"lastMenuIds":lastMenuIdsArr
	  };
	  ajaxCommit({
	     url : ctx + "/sys/role/saveRoleMenu"
	    ,data : params
	  })
  })
  
});